import { Component, OnInit } from '@angular/core';
import { CommonService } from '../../service/common.service'
import { EventService } from '../../service/event.service'

import { Router } from '@angular/router'

@Component({
  selector: 'app-notice',
  templateUrl: './notice.page.html',
  styleUrls: ['./notice.page.scss'],
})
export class NoticePage implements OnInit {

  public noticeList:any[] = [];
  public flag:boolean = false;
  public page:number = 1;
  public size:number = 10;
  
  constructor(private common:CommonService,private router:Router,private eventService:EventService) { }

  ngOnInit() {
    this.notice()

    //监听 实时数据 事件
    this.eventService.event.on('readstatus',(e)=>{
      // 监听是否已经阅读的
      console.log(' 监听是否已经阅读的')
      this.noticeList[e].readStatus = 1
    })
  }
  
  // 通知消息，滚动播出
  notice(){  
    var api = '/tbr/siteNotice/getList?page='+this.page+'&size='+this.size
    this.common.get(api).then((res:any)=>{
      
      if(res.code == 200){
        for(var i=0;i<res.data.length;i++){
          this.noticeList.push(res.data[i])
          // this.changeDetectorRef.detectChanges()
        }
        if(res.data.length < 10){
          this.flag = true
          // this.changeDetectorRef.detectChanges()
        }
        console.log(this.noticeList)
        // this. = res.data
      }
    })
  }

  // 上拉更新
  loadData(event) {
    if (this.flag == true) {
      event.target.disabled = true;
      return
    }
    setTimeout(() => {
      this.page += 1;
      this.notice()
      event.target.complete();
    }, 700);
  }

  

  // 查看详情
  ondetail(e,i){
    
    if(e.readStatus == 0){
      this.eventService.event.emit('readstatus',i)
      this.eventService.event.emit('numbercount')
    }
    this.router.navigate(['/list/ndetail'],{
      queryParams: {noticeId:e.id}
    })
  }

}
